﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace chengz
{
    public delegate void MyDelegate(ChengzDataDto chengzDataDto);

    public partial class rukhp : Form
    {
        DataTable dt = null;
        public event MyDelegate MyEvent;
        // 0,入库回皮; 1, 历史记录
        int flag = 0;

        string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Environment.CurrentDirectory + @"\chengzhongku.mdb";
        OleDbConnection thisConnection = null;

        public rukhp(int flag)
        {
            InitializeComponent();
            this.flag = flag;
        }

        private void rukhp_Load(object sender, EventArgs e)
        {
            thisConnection = new OleDbConnection(connectionString);
            thisConnection.Open();

            string sql = "SELECT czbianhao AS 称重编号, format(createTime, 'yyyy-mm-dd hh:nn:ss') AS 称重时间, yunsgsName AS 运输公司, czchehao AS 运输车辆, czdanwei AS 供应商或项目, czpinming AS 物料, czxinghao AS 规格, " +
                "czmaozhong AS 毛重, czpizhong AS 皮重, czjingzhong AS 净重, czfangshi AS 称重方式, czcrku AS 出入库标记, czrenyuan AS 称重员, remarks AS 备注 " +
                "FROM chengzhong where createTime between #" + dateTimePicker1.Text + " 00:00:00# and #" + dateTimePicker2.Text + " 23:59:59# " +
                "order by createTime desc";

            if(this.flag == 0)
                sql = "SELECT czbianhao AS 称重编号, format(createTime, 'yyyy-mm-dd hh:nn:ss') AS 称重时间, yunsgsName AS 运输公司, czchehao AS 运输车辆, czdanwei AS 供应商或项目, czpinming AS 物料, czxinghao AS 规格, " +
                "czmaozhong AS 毛重, czpizhong AS 皮重, czjingzhong AS 净重, czfangshi AS 称重方式, czcrku AS 出入库标记, czrenyuan AS 称重员, remarks AS 备注 " +
                "FROM chengzhong where czpizhong = 0 and createTime between #" + dateTimePicker1.Text + " 00:00:00# and #" + dateTimePicker2.Text + " 23:59:59# " +
                "order by createTime desc";

            OleDbDataAdapter thisAdapter = new OleDbDataAdapter(sql, thisConnection);
            System.Data.DataSet thisDataSet = new System.Data.DataSet();
            thisAdapter.Fill(thisDataSet, "table");
            dt = thisDataSet.Tables["table"];
            this.dataGridView1.DataSource = dt;
        }

        private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            var abc = dt.Rows[e.RowIndex];

            ChengzDataDto chengzDataDto = new ChengzDataDto();
            chengzDataDto.称重编号 = abc["称重编号"].ToString();
            chengzDataDto.称重时间 = abc["称重时间"].ToString();
            chengzDataDto.供应商或项目 = abc["供应商或项目"].ToString();
            chengzDataDto.物料 = abc["物料"].ToString();
            chengzDataDto.规格 = abc["规格"].ToString();
            chengzDataDto.运输公司 = abc["运输公司"].ToString();
            chengzDataDto.运输车辆 = abc["运输车辆"].ToString();
            chengzDataDto.毛重 = abc["毛重"].ToString();
            chengzDataDto.皮重 = abc["皮重"].ToString();
            chengzDataDto.净重 = abc["净重"].ToString();
            chengzDataDto.称重方式 = abc["称重方式"].ToString();
            chengzDataDto.出入库标记 = abc["出入库标记"].ToString();
            chengzDataDto.称重员 = abc["称重员"].ToString();
            chengzDataDto.备注 = abc["备注"].ToString();
            chengzDataDto.类型 = this.flag.ToString();

            this.MyEvent(chengzDataDto);
            this.Close();
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //自动编号，与数据无关
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
               e.RowBounds.Location.Y,
               dataGridView1.RowHeadersWidth - 4,
               e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                  (e.RowIndex + 1).ToString(),
                   dataGridView1.RowHeadersDefaultCellStyle.Font,
                   rectangle,
                   dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
                   TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sql = "SELECT czbianhao AS 称重编号, format(createTime, 'yyyy-mm-dd hh:nn:ss') AS 称重时间, yunsgsName AS 运输公司, czchehao AS 运输车辆, czdanwei AS 供应商或项目, czpinming AS 物料, czxinghao AS 规格, " +
                "czmaozhong AS 毛重, czpizhong AS 皮重, czjingzhong AS 净重, czfangshi AS 称重方式, czcrku AS 出入库标记, czrenyuan AS 称重员, remarks AS 备注 " +
                "FROM chengzhong where createTime between #" + dateTimePicker1.Text + " 00:00:00# and #" + dateTimePicker2.Text + " 23:59:59# " +
                "order by createTime desc";

            if (this.flag == 0)
                sql = "SELECT czbianhao AS 称重编号, format(createTime, 'yyyy-mm-dd hh:nn:ss') AS 称重时间, yunsgsName AS 运输公司, czchehao AS 运输车辆, czdanwei AS 供应商或项目, czpinming AS 物料, czxinghao AS 规格, " +
                "czmaozhong AS 毛重, czpizhong AS 皮重, czjingzhong AS 净重, czfangshi AS 称重方式, czcrku AS 出入库标记, czrenyuan AS 称重员, remarks AS 备注 " +
                "FROM chengzhong where czpizhong = 0 and createTime between #" + dateTimePicker1.Text + " 00:00:00# and #" + dateTimePicker2.Text + " 23:59:59# " +
                "order by createTime desc";

            OleDbDataAdapter thisAdapter = new OleDbDataAdapter(sql, thisConnection);
            System.Data.DataSet thisDataSet = new System.Data.DataSet();
            thisAdapter.Fill(thisDataSet, "table");
            dt = thisDataSet.Tables["table"];
            this.dataGridView1.DataSource = dt;

            dt.DefaultView.RowFilter = "称重编号 like '*" + textBox_czbh.Text + "*'";
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                DialogResult dr = MessageBox.Show(this, "确认删除么？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.OK)
                {
                    DataRowView drv = dataGridView1.SelectedRows[0].DataBoundItem as DataRowView;
                    string czbianhao = drv.Row[0] + "";

                    OleDbCommand cmd = thisConnection.CreateCommand();
                    cmd.CommandText = "delete from chengzhong where czbianhao = '" + czbianhao + "'";

                    try
                    {
                        if (cmd.ExecuteNonQuery() > 0)
                        {
                            MessageBox.Show("操作成功", "提示");
                            drv.Row.Delete();
                        }
                        else
                            MessageBox.Show("操作失败", "提示");

                    }
                    catch (Exception error)
                    {
                        MessageBox.Show(error.Message, "提示");
                    }
                }

            }
            else
                MessageBox.Show("请选择要删除的记录！", "提示");
        }
    }
}
